﻿using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TextTemplating;

namespace MVCScaffold.Extensions
{
    [Serializable()]
    public class MvcScaffoldTextTemplateHost : ITextTemplatingEngineHost
    {
        public MvcScaffoldTextTemplateHost(string templateFileValue)
        {
            TemplateFile = templateFileValue;
        }
        public CompilerErrorCollection Errors { get; private set; }
        public string FileExtension { get; private set; }
        public Encoding FileEncoding { get; private set; }
        public string EntityName { get; set; }
        public string Namespace { get; set; }
        public string ContextName { get; set; }
        public string Update { get; set; }
        public string EditQuery { get; set; }
        public string Key { get; set; }
        public string EdmxNamespace { get; set; }
        public string VarContext { get; set; }
        public string Query { get; set; }
        public List<Property> Propriedades { get; set; }


        #region ITextTemplatingEngineHost Members

        public object GetHostOption(string optionName)
        {
            object returnObject;
            switch (optionName)
            {
                case "CacheAssemblies":
                    returnObject = true;
                    break;
                default:
                    returnObject = null;
                    break;
            }
            return returnObject;
        }

        public bool LoadIncludeText(string requestFileName, out string content, out string location)
        {
            content = String.Empty;
            location = String.Empty;

            if (File.Exists(requestFileName))
            {
                content = File.ReadAllText(requestFileName);
                return true;
            }

            return false;
        }

        public void LogErrors(CompilerErrorCollection errors)
        {
            Errors = errors;
        }

        public AppDomain ProvideTemplatingAppDomain(string content)
        {
            return AppDomain.CurrentDomain;
        }

        public string ResolveAssemblyReference(string assemblyReference)
        {
            if (File.Exists(assemblyReference))
            {
                return assemblyReference;
            }

            string candidate = Path.Combine(Path.GetDirectoryName(TemplateFile), assemblyReference);
            if (File.Exists(candidate))
            {
                return candidate;
            }

            return "";
        }

        public Type ResolveDirectiveProcessor(string processorName)
        {
            return null;
        }

        public string ResolveParameterValue(string directiveId, string processorName, string parameterName)
        {
            return String.Empty;
        }

        public string ResolvePath(string path)
        {
            if (path == null)
                throw new ArgumentNullException("the file name cannot be null");

            if (File.Exists(path))
                return path;

            string candidate = Path.Combine(Path.GetDirectoryName(TemplateFile), path);
            if (File.Exists(candidate))
                return candidate;

            return path;
        }

        public void SetFileExtension(string extension)
        {
            FileExtension = extension;
        }

        public void SetOutputEncoding(Encoding encoding, bool fromOutputDirective)
        {
            FileEncoding = encoding;
        }

        public IList<string> StandardAssemblyReferences
        {
            get
            {
                return new[]
                {
                    typeof(Uri).Assembly.Location,
                    typeof(Queryable).Assembly.Location,
                    typeof(MvcScaffoldTextTemplateHost).Assembly.Location
                };
            }
        }

        public IList<string> StandardImports
        {
            get
            {
                return new[]
                {
                    "System",
                    "System.IO",
                    "System.Diagnostics",
                    "System.Linq",
                    "System.Collections",
                    "System.Collections.Generic",
                    "MVCScaffold.Extensions"
                };
            }
        }

        public string TemplateFile
        {
            get;
            private set;
        }

        #endregion


    }
}
